package com.lqzs.businessmanager.adapter

import android.annotation.SuppressLint
import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import androidx.constraintlayout.widget.ConstraintLayout
import androidx.recyclerview.widget.RecyclerView
import com.lqzs.businessmanager.Config
import com.lqzs.businessmanager.R
import com.lqzs.businessmanager.bean.response.LoginShopResponse
import com.lqzs.businessmanager.utils.PreferencesUtil

class BusinessAdapter (businessData: List<LoginShopResponse>, context: Context): RecyclerView.Adapter<BusinessAdapter.BusinessAdapterHolder>() {

    private var mBusinessData: List<LoginShopResponse> = businessData
    private var context: Context = context

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): BusinessAdapterHolder {
        val inflate: View = LayoutInflater.from(parent.context).inflate(R.layout.item_business, parent, false)
        return BusinessAdapterHolder(inflate)
    }

    @SuppressLint("SetTextI18n")
    override fun onBindViewHolder(holder: BusinessAdapterHolder, position: Int) {
        val mData: LoginShopResponse = mBusinessData[position]

        holder.tvVendorName.text = "${mData.vendor_name}"
        holder.tvVendorId.text = "${mData.vendor_id}"
        holder.tvPlatformName.text = "${mData.platform_name}"

        when(mData.platform_name){
            "美团外卖"->{
                holder.imgLogo.setImageResource(R.drawable.mt_logo)
            }
            "饿了么"->{
                holder.imgLogo.setImageResource(R.drawable.ele_logo)
            }
            "美团到店"->{
                holder.imgLogo.setImageResource(R.drawable.mtdp_logo)
            }
        }


        holder.tvPlatformID.text = "${mData.platform_id}"
        mData.expire_time?.let {
            holder.tvExpireTime.text = "到期时间 : $it"
        }
        holder.tvAddress.text = "${mData.address}"
        holder.imgLogo.setOnClickListener {
            PreferencesUtil.putString(context, Config.BUSINESS_SHOP_ID,"${mData.branch_id}")
            PreferencesUtil.putString(context, Config.BUSINESS_VENDOR_ID,"${mData.vendor_id}")
            PreferencesUtil.putString(context, Config.BUSINESS_PLATFORM_ID,"${mData.platform_id}")
            onItemAdapterClickListener?.onClick(position,mData)
        }
        holder.item.setOnClickListener{
            PreferencesUtil.putString(context, Config.BUSINESS_SHOP_ID,"${mData.branch_id}")
            PreferencesUtil.putString(context, Config.BUSINESS_VENDOR_ID,"${mData.vendor_id}")
            PreferencesUtil.putString(context, Config.BUSINESS_PLATFORM_ID,"${mData.platform_id}")
            onItemAdapterClickListener?.onClick(position,mData)
        }
    }

    override fun getItemCount(): Int {
        return mBusinessData.size
    }
    
    class BusinessAdapterHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        var tvVendorName: TextView = itemView.findViewById(R.id.tv_vendorName)
        var tvVendorId: TextView = itemView.findViewById(R.id.tv_vendorId)
        var tvPlatformName: TextView = itemView.findViewById(R.id.tv_platformName)
        var tvPlatformID: TextView = itemView.findViewById(R.id.tv_platformID)
        var tvExpireTime: TextView = itemView.findViewById(R.id.tv_expireTime)
        var tvAddress: TextView = itemView.findViewById(R.id.tv_address)
        var imgLogo : ImageView = itemView.findViewById(R.id.img_logo)
        var item: ConstraintLayout = itemView.findViewById(R.id.itemView)
    }

    private var onItemAdapterClickListener: OnItemAdapterClickListener? = null


    fun setOnItemAdapterClickListener(onItemAdapterClickListener: OnItemAdapterClickListener?) {
        this.onItemAdapterClickListener = onItemAdapterClickListener
    }

    interface OnItemAdapterClickListener {
        fun onClick(position: Int, mData: LoginShopResponse)
    }
}